package com.xtaller.easy.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.*;
import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysMenuMapper;
import com.xtaller.easy.admin.model.SysMenu;
import com.xtaller.easy.admin.model.SysMenuAuth;
import com.xtaller.easy.admin.model.SysModule;
import com.xtaller.easy.admin.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.xtaller.common.bean.Where;
/**
* Created by Taller on 2018/04/13
*/
@Service
public class SysMenuService extends TServiceImpl<SysMenuMapper,SysMenu> implements ISysMenuService {
    @Autowired
    private SysModuleService moduleService;
   /**************************CURD begin******************************/ 
   // 创建
   @Override
   public SysMenu createSysMenu(SysMenu model) {
       if(this.insert(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 删除
   @Override
   public Boolean deleteSysMenu(Object ids,String reviser) {
       return this.delete(ids,reviser);
   }
 
   // 修改
   @Override
   public SysMenu updateSysMenu(SysMenu model) {
       if(this.update(model))
           return this.selectById(model.getId());
       return null;
   }
 
   // 查询
   @Override
   public List<SysMenu> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }



    /**************************CURD end********************************/
    @Override
    public Boolean existName(SysMenu model, boolean isCreate) {
        if(!isCreate){
            where = W.f(
                    W.and("name", "eq", model.getName()),
                    W.and("parentId", "eq", model.getParentId()),
                    W.and("id", "ne", model.getId())
            );
        }else{
            where = W.f(
                    W.and("name", "eq", model.getName()),
                    W.and("parentId", "eq", model.getParentId())
            );
        }
        return this.queryOne(where) == null ? false : true;
    }

    @Override
    public List<JSONObject> getMenuTreeByModuleId(Long moduleId) {
        List<SysMenu> menus = this.query(W.f(
                W.and("moduleId", "eq", moduleId),
                W.field("id", "name", "moduleId", "parentId", "icon", "url", "api", "sort", "isHidden"),
                W.order("sort", "asc")
        ));
        return J.o2l(L.list2Tree(J.o2l(menus), "0"));
    }

    @Override
    public Boolean isDelete(String id) {
        List<SysMenu> menus = this.query(W.f(
                W.and("parentId", "eq", id)
        ));
        return menus.size() > 0 ? true : false;
    }

    @Override
    public List<JSONObject> getMenu() {
        // 获取模块
        List<SysModule> modules = getAllModule();
        // 获取所有菜单
        List<SysMenu> menus = getAllMenu();
        // 获取所有权限按钮
        List<JSONObject> buttons = getAllAuth();
        List<JSONObject> menuData = formatButton2Menu(menus, buttons);

        return formatMenu2Module(modules, menuData);
    }
    // 组装模块与菜单数据
    private List<JSONObject> formatMenu2Module(List<SysModule> modules, List<JSONObject> menuData){
        List<JSONObject> moduleData = new ArrayList<>();
        if(modules.size() > 0){
            for(SysModule module:modules){
                List<JSONObject> menus = new ArrayList<>();
                JSONObject object = new JSONObject();
                object.put("id", module.getId());
                object.put("name", module.getName());
                object.put("text", module.getName());
                object.put("code", module.getCode());
                object.put("sort", module.getSort());
                object.put("certified", module.getCertified());
                if(menuData.size() > 0){
                    for(JSONObject menu:menuData){
                        if(V.isEqual(module.getId(), menu.getString("moduleId"))){
                            menus.add(menu);
                        }
                    }
                }
                object.put("menus", menus);
                moduleData.add(object);
            }
        }
        return moduleData;
    }
    // 组装菜单与按钮数据
    private List<JSONObject> formatButton2Menu(List<SysMenu> menus, List<JSONObject> buttons){
        // 组装菜单数据
        List<JSONObject> menuData = new ArrayList<>();
        if(menus.size() > 0){
            for(SysMenu menu:menus){
                JSONObject m = new JSONObject();
                List<JSONObject> btns = new ArrayList<>();
                m.put("id", menu.getId());
                m.put("name", menu.getName());
                m.put("text", menu.getName());
                m.put("moduleId", menu.getModuleId());
                m.put("parentId", menu.getParentId());
                m.put("icon", menu.getIcon());
                m.put("url", menu.getUrl());
                m.put("api", menu.getApi());
                m.put("sort", menu.getSort());
                m.put("isHidden", menu.getIsHidden());
                m.put("remark", menu.getRemark());

                if(menuData.size() > 0){
                    for(JSONObject button:buttons){
                        if(V.isEqual(menu.getId(), button.getString("menuId"))){
                            btns.add(button);
                        }
                    }
                }

                m.put("btns", btns);
                menuData.add(m);
            }
        }
        return menuData;
    }
    private List<SysMenu> getAllMenu(){
        return this.query(
                W.f(
                        W.field("id", "name", "moduleId", "parentId", "icon", "url", "api", "isHidden", "remark", "sort"),
                        W.order("sort", "asc")
                )
        );
    }
    private List<SysModule> getAllModule(){
        return moduleService.query(
                W.f(
                    W.field("id", "name", "code", "remark", "sort"),
                    W.order("sort", "asc")
                )
        );
    }
    private List<JSONObject> getAllAuth(){
        return F.f2l(baseMapper.getAllAuth(), "id", "menuId", "buttonId");
    }

}